package cn.com.shadowless.xinning.view.fragment;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.mengpeng.mphelper.ToastUtils;

import java.util.List;

import butterknife.BindView;
import cn.com.shadowless.xinning.MyApplication;
import cn.com.shadowless.xinning.R;
import cn.com.shadowless.xinning.callback.RequestCallBack;
import cn.com.shadowless.xinning.model.RequestResult;
import cn.com.shadowless.xinning.model.order.ProcureOrderInfo;
import cn.com.shadowless.xinning.presenter.OrderPresenter;
import cn.com.shadowless.xinning.utils.PreferencesUtils;
import cn.com.shadowless.xinning.view.BaseFragment;

/**
 * The type Procure fragment.
 *
 * @author sHadowLess
 */
public class ProcureFragment extends BaseFragment {
    /**
     * The Buy order num.
     */
    @BindView(R.id.buy_orderNum)
    TextView buyOrderNum;
    /**
     * The Buy make time.
     */
    @BindView(R.id.buy_makeTime)
    TextView buyMakeTime;
    /**
     * The Buy maker.
     */
    @BindView(R.id.buy_maker)
    TextView buyMaker;
    /**
     * The Buy make depart.
     */
    @BindView(R.id.buy_makeDepart)
    TextView buyMakeDepart;
    /**
     * The Buy buy duty.
     */
    @BindView(R.id.buy_buyDuty)
    TextView buyBuyDuty;
    /**
     * The Buy depart name.
     */
    @BindView(R.id.buy_departName)
    TextView buyDepartName;
    /**
     * The Buy remark.
     */
    @BindView(R.id.buy_remark)
    TextView buyRemark;
    /**
     * The Buy supplier.
     */
    @BindView(R.id.buy_supplier)
    TextView buySupplier;
    /**
     * The Buy record.
     */
    @BindView(R.id.buy_record)
    TextView buyRecord;
    /**
     * The Buy buy info.
     */
    @BindView(R.id.buy_buyInfo)
    LinearLayout buyBuyInfo;
    /**
     * The Buy check process.
     */
    @BindView(R.id.buy_check_process)
    LinearLayout buyCheckProcess;

    private final String url = "api/PublicResourcePurchaseOrder/LoadPurchaseOrderCheckHistory?%s=";

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_procurement_layout;
    }

    @Override
    protected void initData() {
        Bundle bundle = getArguments();
        if (null != bundle) {
            String orderId = bundle.getString("orderId");
            String userName = bundle.getString("userName");
            initProcureOrderInfo(orderId, userName);
            initCheckHistoryList(String.format(url, "orderId") + orderId, mActivity, buyCheckProcess);
        }
    }

    private void initProcureOrderInfo(String orderId, String userName) {
        OrderPresenter
                .getSingleton()
                .getProcureOrderInfo(orderId, userName, new RequestCallBack<ProcureOrderInfo>() {
                    @Override
                    public void success(RequestResult<ProcureOrderInfo> result) {
                        String token = result.getLoginToken();
                        String oldToken = PreferencesUtils.getString(MyApplication.getInstance(), "token", "-1");
                        if (result.isSucceed() && null != result.getResult()) {
                            if (null != token && token.equals(oldToken)) {
                                Log.e(TAG, "success: " + result.toString());
                                ProcureOrderInfo.DataDTO dataBean = result.getResult().getData();
                                List<ProcureOrderInfo.DataDTO.ItemListDTO> itemListBeans = dataBean.getItemList();

                                String id = dataBean.getId();
                                String createTimeStr = dataBean.getCreateTimeStr();
                                String createUserName = dataBean.getCreateUserName();
                                String departmentName = dataBean.getDepartmentName();
                                String purchaserName = dataBean.getPurchaserName();
                                String remark = dataBean.getRemark();
                                String partnerName = dataBean.getPartnerName();
                                String enquiryInfo = dataBean.getEnquiryInfo();
                                buyOrderNum.setText(id);
                                buyMakeTime.setText(createTimeStr);
                                buyMaker.setText(createUserName);
                                buyMakeDepart.setText(departmentName);
                                buyBuyDuty.setText(purchaserName);
                                buyDepartName.setText(departmentName);
                                buyRemark.setText(remark);
                                buySupplier.setText(partnerName);
                                buyRecord.setText(enquiryInfo);

                                for (ProcureOrderInfo.DataDTO.ItemListDTO itemListBean : itemListBeans) {
                                    View view = LayoutInflater.from(mActivity).inflate(R.layout.fragment_procure_tab_item, null, false);
                                    LinearLayout tab = view.findViewById(R.id.procure_tab_Item);
                                    TextView procureTabProject = view.findViewById(R.id.procure_tab_project);
                                    TextView procureTabBuyNum = view.findViewById(R.id.procure_tab_buyNum);
                                    TextView procureTabCost = view.findViewById(R.id.procure_tab_Cost);
                                    TextView procureTabType = view.findViewById(R.id.procure_tab_type);
                                    TextView procureTabSize = view.findViewById(R.id.procure_tab_size);
                                    TextView procureTabUnit = view.findViewById(R.id.procure_tab_unit);
                                    TextView procureTabThingType = view.findViewById(R.id.procure_tab_thingType);
                                    String publicResourceItemName = itemListBean.getPublicResourceItemName();
                                    String brand = itemListBean.getBrand();
                                    String size = itemListBean.getSize();
                                    String publicResourceName = itemListBean.getPublicResourceName();
                                    String unit = itemListBean.getUnit();
                                    String num = itemListBean.getNum() + "";
                                    String price = getDecimal(itemListBean.getPrice().doubleValue());
                                    procureTabThingType.setText(publicResourceName);
                                    procureTabType.setText(brand);
                                    procureTabSize.setText(size);
                                    procureTabUnit.setText(unit);
                                    procureTabProject.setText(publicResourceItemName);
                                    procureTabBuyNum.setText(num);
                                    procureTabCost.setText(price);
                                    buyBuyInfo.addView(tab);
                                }
                            } else {
                                ToastUtils.onErrorShowToast("登录态与服务器不一致");
                                initLogoutInfo(mActivity, userName);
                            }
                        } else {
                            ToastUtils.onErrorShowToast("该订单暂无详细信息");
                        }
                    }

                    @Override
                    public void fail(String errorMsg) {
                        ToastUtils.onErrorShowToast(errorMsg);
                    }
                });
    }

    @Override
    protected void initView() {

    }
}
